package com.glorysoft.xztpda.ui.trackin

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.bindingadapter.CarrierCheckAdapter
import com.glorysoft.xztpda.databinding.ActivityTrackInCarrierBinding
import com.glorysoft.xztpda.entity.LotInfoEntity
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import kotlinx.android.synthetic.main.activity_or_comment.*
import kotlinx.android.synthetic.main.activity_track_in_carrier.*
import kotlinx.android.synthetic.main.activity_track_in_guide.*

/**
 * FileName: TrackInCarrier
 * Author: XJH
 * Date: 2022/4/12 17:04
 * Description:
 * History:
 */
class TrackInCarrierActivity : BaseActivity<TrackInBaseViewModel,ActivityTrackInCarrierBinding>(),
    View.OnClickListener {

    private var mCarrierAdapter : CarrierCheckAdapter? = null


    override fun layoutId(): Int {
        return R.layout.activity_track_in_carrier
    }

    override fun initVariableId(): Int {
        return BR.trackInBaseViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
//        setTitle("开始作业")
        viewModel.lotInfoEntity = intent.getSerializableExtra("LOTINFO") as LotInfoEntity
        setTitle("开始作业" + "-" +viewModel.lotInfoEntity?.lot?.stepdesc)
        addActivity(this)
        setRightListener("提交"){
            val checkList = mCarrierAdapter!!.checkList
            for (i in 0 until checkList.size()) {
                if (!checkList[i]) {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("当前有载具未校验",false)
                    return@setRightListener
                }
            }

            viewModel.trackIn("CarriersCherkTrackIn")
        }
    }

    override fun initData() {

        mCarrierAdapter = CarrierCheckAdapter(viewModel.lotInfoEntity?.durablelist)
        rvCarrierList.adapter = mCarrierAdapter
        scanCarrier()

        viewModel.defUI.startActivity.observe(this){
            finishAllActivity()
        }

        ivCarrierScan.setOnClickListener(this)
    }

    /**
     * 载具验证
     */
    private fun scanCarrier() {
        initEdit(etCarrier,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                etCarrier.setText("")
                Handler().postDelayed({ etCarrier.requestFocus() }, 500)
                checkCarrier(s)
            }

        })
    }

    private fun checkCarrier(s : String) {
        if (mCarrierAdapter?.data == null || mCarrierAdapter?.data!!.size <= 0) {
            return
        }

        val data = mCarrierAdapter?.data
        for (i in data?.indices!!) {
            if (data[i].equals(s, ignoreCase = true)) {
                if (mCarrierAdapter?.checkList?.get(i)!!) {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("当前载具已校验",false)
                } else {
                    mCarrierAdapter?.setCheck(s)
                }
                return
            }
        }
        viewModel.defUI.toastEvent.value = ToastMessageBean("当前载具不匹配",false)
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.ivCarrierScan -> startScanForCode(this,10001)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        // 当扫码页面结束后，处理扫码结果。
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK || data == null) {
            return
        }
        if (resultCode == RESULT_OK){
            // 从onActivityResult返回data中，用ScanUtil.RESULT作为key值取到HmsScan返回值。
            when (val obj: Any = data.getParcelableExtra(ScanUtil.RESULT)) {
                is HmsScan -> {
                    val barCode = obj.getOriginalValue()
                    when(requestCode){
                        10001 -> {
                            checkCarrier(barCode)
                        }
                    }

                }
            }
        }
    }
}